<?php
class DataTableRendererEdit extends DataTableRenderer
{
	public function getHTML()
	{
		$html = "<SCRIPT language='javascript'>\n";
		$html .= "function ".$this->dataTable->getName()."Refresh()\n";
		$html .= "{\n";
//		$html .= "document.getElementById(\"Action\").value = \"".$this->action."\";\n";
		$html .= "document.getElementById(\"PageForm\").submit();\n";
		$html .= "}\n";
		$html .= "</SCRIPT>\n";

		if(isset($this->subAction))
		{
			$hidden = new Hidden();
			$hidden->setName("SubAction");
			$hidden->setValue($this->subAction);
			$html .= $hidden->getHTML();
		}

		$hidden = new Hidden();
		$hidden->setName("ShowSearchCriteria");
		$hidden->setValue('table');
		$html .= $hidden->gethtml();

		$RecordID = new Hidden();
		$RecordID->setName("RecordID");
		$html .= $RecordID->getHTML();

		$DBTableName = new Hidden();
		$DBTableName->setName("DBTableName");
		$html .= $DBTableName->getHTML();
		
		$html .= "<script language=\"javascript\">\n";
		$html .= "function toggleFilterPanel()\n";
		$html .= "{\n";
//		$html .= "alert (\"Hello\");\n";
		$html .= "criteria = document.getElementById(\"ShowSearchCriteria\");\n";
		$html .= "table = document.getElementById(\"filterPanel\");\n";
		$html .= "text = document.getElementById(\"filterText\");\n";
//		$html .= "alert (text.innerHTML);\n";
		$html .= "if(text.innerHTML == 'Click to show search criteria')\n";
		$html .= "{\n";
		$html .= "table.style.display = 'table';\n";
		$html .= "text.innerHTML = 'Click to hide search criteria'\n";
		$html .= "criteria.value='table';\n";
		$html .= "}\n";
		$html .= "else\n";
		$html .= "{\n";
		$html .= "table.style.display = 'none';\n";
		$html .= "text.innerHTML = 'Click to show search criteria'\n";
		$html .= "criteria.value='none';\n";
		$html .= "}\n";
		$html .= "}\n";
		$html .= "</script>\n";
		
		if($this->dataTable->getAreEditable())
		{
			$newButton1 = new Button();
			$newButton1->setName("newButton");
			$newButton1->setValue("New Record");
			$newButton1->setOnClick("newRecord('".$this->dataTable->getDBTableName()."');");
			$html .= "<tr><td>".$newButton1->getHTML()."</tr></td>";
		}
		
		$requests = $this->dataTable->getFilterRequests();
//		echo count($requests)."<br>";
		if(count($requests)!=0)
		{
			$display = 'none';
			if(isset($_POST['ShowSearchCriteria']))
				$display = $_POST['ShowSearchCriteria'];
	
			$text = ($display=='none')?"Click to show search criteria":"Click to hide search criteria";
			
				
			$html .= "<h3><a id=\"filterText\" href='javascript:toggleFilterPanel()'>".$text."</a></h3>\n";
			$html .= "<p><table style=\"{display:".$display.";}\" id=\"filterPanel\">";
	
			// Filter Requests
	
			foreach ($this->dataTable->getFilterRequests() as $aFilterRequest)
			{
				$html .= $aFilterRequest->getHTML();
			}

			if(1)
//			if($this->dataTable->getAreEditable())
			{
				$refreshButton1 = new Button();
				$refreshButton1->setName("refreshButton");
				$refreshButton1->setValue("Refresh");
				$refreshButton1->setOnClick($this->dataTable->getName()."Refresh('".$this->dataTable->getDBTableName()."');");
				$html .= "<tr><td>".$refreshButton1->getHTML()."</tr></td>";
			}
	
			$html .= "</table></p>";
		}

		$html .= "<table>";
		
		$conditions = "";

		$this->dataTable->getDatabase()->ExecuteSQL($this->dataTable->getSQLCommand());
//		echo $this->dataTable->getSQLCommand()."<br>\n";
		$html .= "<tr>";
		foreach($this->dataTable->getColumns() as $column)
		{
			$html .= "<th>".$column->getTitle()."</th>";
		}
		$html .= "</tr>";
		
		$selectedRows = array();
		if($this->dataTable->getAreSelectable())
			{
				$database = $this->dataTable->getDatabase()->Recopy();
				$sql = "SELECT * FROM ".$this->dataTable->getDBTableName();
//				echo $sql."<br>";
				$database->ExecuteSQL($sql);
//				echo "number of rows = ".$database->getNumRows()."<br>";
				while($row=$database->getRow())
				{
					array_push($selectedRows,$row[substr($this->dataTable->getSelectSourceTable(),0,-1)."ID"]);
				}
			}
		
		
		
		while($row=$this->dataTable->getDatabase()->getRow())
		{
//			echo mysql_error();


			$html .= "<tr>";
			foreach($this->dataTable->getColumns() as $column)
			{
				$DeletedFontText = "";
				if(isset($row["DeletedBy"]))
				{
					if ($row["DeletedBy"]!='0') $DeletedFontText = " style=\"color: #dd0000\"";
				}
				$html .= "\n<span>\n";
				switch ($column->getDisplayAs())
				{
					case 'DateOnly':
						$date = new DateTime($row[$column->getField()]);
						$html .= "<td$DeletedFontText>".str_replace(' ','&nbsp;',$date->format("D d M Y"))."</td>";
						break;

					case 'TimeOnly':
						$date = new DateTime($row[$column->getField()]);
						$html .= "<td$DeletedFontText>".$date->format("H:i")."</td>";
						break;

					case 'DateTime':
						$date = new DateTime($row[$column->getField()]);
						$html .= "<td$DeletedFontText>".$date->format("d M Y  H:i")."</td>";
						break;

					case 'SingleEnumeration':
						$field = $column->getField();
//						echo $field."<br>";
						$sql = "select ".substr($field,0,-2)." FROM ".substr($field,0,-2)."s WHERE ".$field."=".$row[$field];
//						echo $sql."<br>";
						$database = $this->dataTable->getDatabase()->Recopy();
						$database->ExecuteSQL($sql);
						$row1 = $database->getRow();
						$html .= "<td$DeletedFontText>".$row1[substr($field,0,-2)]."</td>";
						break;

					case 'LongNameEnumeration':
						$field = $column->getField();
//						echo $field."<br>";
						$sql = "select LongName FROM ".substr($field,0,-2)."s WHERE ".$field."=".$row[$field];
//						echo $sql."<br>";
						$database = $this->dataTable->getDatabase()->Recopy();
						$database->ExecuteSQL($sql);
						$row1 = $database->getRow();
						$html .= "<td$DeletedFontText>".$row1['LongName']."</td>";
						break;

					case 'MultiEnumeration':
						$field = $column->getField();
						$sql = "SELECT id.".$field."NameID, n.ShortName FROM ".$field."s as id INNER JOIN ".$field."Names as n ON id.".$field."NameID = n.".$field."NameID WHERE id.EventID = ".$row['ColumnID'];
//						echo $sql;
						$database = $this->dataTable->getDatabase()->Recopy();
						$database->ExecuteSQL($sql);
						$result = "";
						while($row1 = $database->getRow())
					{
							$result.=$row1["ShortName"]." ";
//							print_r ($row1);
					}
						$html .= "<td$DeletedFontText>".$result."</td>";
						break;

						default:
						$html .= "<td$DeletedFontText>".$row[$column->getField()]."</td>";
						break;	
						
					case 'BodgeMultiEnumeration': //only used by CMR
						$field = $column->getField();
						$sql = "SELECT id.UserID, n.ShortName FROM Actions as id INNER JOIN Users as n ON id.UserID = n.UserID WHERE id.ResourceID = ".$row['ColumnID']." AND id.ActionTypeID = 2";
		//				echo $sql;
						$database = $this->dataTable->getDatabase()->Recopy();
						$database->ExecuteSQL($sql);
						$result = "";
						while($row1 = $database->getRow())
					{
							$result.=$row1["ShortName"]." ";
//							print_r ($row1);
					}
						$html .= "<td$DeletedFontText>".$result."</td>";
						break;

						default:
						$html .= "<td$DeletedFontText>".$row[$column->getField()]."</td>";
						break;	
				}
				$html .= "\n</span>\n";
			}
			if($this->dataTable->getAreViewable())
			{
				if($this->dataTable->getEditDeleteType() == "Button")
				{
					$viewButton = new Button();
					$viewButton->setName("viewButton");
					$viewButton->setValue("View");
					$viewButton->setOnClick("viewRecord('".$row['ColumnID']."','".$this->dataTable->getDBTableName()."');");
					$html .= "<td>".$viewButton->getHTML()."</td>\n";
				}
				else
				{
					$html .= "<td><a href=\"javascript:viewRecord('".$row['ColumnID']."','".$this->dataTable->getDBTableName()."')\">View</a></td>\n";
				}
			}

			if($this->dataTable->getAreEditable())
			{
				if($this->dataTable->getEditDeleteType() == "Button")
				{
					$editButton = new Button();
					$editButton->setName("editButton");
					$editButton->setValue("Edit");
					$editButton->setOnClick("editRecord('".$row['ColumnID']."','".$this->dataTable->getDBTableName()."');");
					$html .= "<td>".$editButton->getHTML()."</td>\n";
				}
				else
				{
					$html .= "<td><a href=\"javascript:editRecord('".$row['ColumnID']."','".$this->dataTable->getDBTableName()."')\">Edit</a></td>\n";
				}
			}

			if($this->dataTable->getAreDeletable())
			{
				if($this->dataTable->getEditDeleteType() == "Button")
				{
					$deleteButton = new Button();
					$deleteButton->setName("deleteButton");
					$deleteButton->setValue("Delete");
					$deleteButton->setOnClick("javascript:deleteRecord('".$row['ColumnID']."','".$this->dataTable->getDBTableName()."');");
					$html .= "<td>".$deleteButton->getHTML()."</td>";
				}
				else
				{
					$html .= "<td><a href=\"javascript:deleteRecord('".$row['ColumnID']."','".$this->dataTable->getDBTableName()."')\">Delete</a></td>\n";
				}
			}

			if($this->dataTable->getAreSelectable())
			{
				$selectCheckbox = new Checkbox();
				$selectCheckbox->setName("SubmitSelect_".$this->dataTable->getName()."_".$row['ColumnID']);
				$selectCheckbox->setValue("Select".$row['ColumnID']);
//				$selectCheckbox->setOnClick("javascript:submitSelectForm();");
//				print_r ($selectedRows);
//				echo "<br>\n";
//				echo "columnID = ".$row['ColumnID']."<br>\n";
				if (in_array($row['ColumnID'],$selectedRows))
					$selectCheckbox->setChecked(true);
				$html .= "<td>".$selectCheckbox->getHTML()."</td>";
			}

			$html .= "</tr>";
		}

		if($this->dataTable->getAreEditable())
		{
			$newButton = new Button();
			$newButton->setName("newButton");
			$newButton->setValue("New");
			$newButton->setOnClick("newRecord('".$this->dataTable->getDBTableName()."');");
			$html .= "<tr><td>".$newButton->getHTML()."</tr></td>";
		}

		$html .= "</table>";

		if($this->dataTable->getAreSelectable())
		{
			$updateButton = new Button();
			$updateButton->setName("updateButton");
			$updateButton->setValue("Update");
			$updateButton->setOnClick("javascript:submitSelectForm();");
						$html .= "<tr><td>".$updateButton->getHTML()."</tr></td>";
		}

		return $html;

	}

}

?>